from flask import Flask, jsonify, request
from dataManagement.DataInterface import DataInterface
from dataManagement.MetaDataInterface import MetaDataInterface
from featuresConstruct.PearsonAnalysis import Pearson
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False  # 禁止中文转义





DataInterface = DataInterface()
MetaDataInterface = MetaDataInterface()
pearson = Pearson()
@app.route("/user/login", methods=["POST"])
def user_login():
    """
    用户登录
    :return:
    """
    data = request.get_json()
    userName = data.get("userName")
    password = data.get("password")
    if userName == "admin" and password == "123456":
        return jsonify({
            "code": 0,
            "data": {
                "token": "666666",
                "userName": "dxs",
                "password": "passwordf"
            }
        })
    else:
        return jsonify({
            "code": 99999999,
            "msg": "用户名或密码错误"
        })


@app.route("/user/info", methods=["GET", "POST"])
def user_info():
    """
    获取当前用户信息
    :return:
    """
    token = request.headers.get("token")
    if token == "666666":
        return jsonify({
            "code": 0,
            "data": {
                "id": "1",
                "userName": "admin",
                "realName": "张三",
                "userType": 2
            }
        })
    return jsonify({
        "code": 99990403,
        "msg": "token不存在或已过期"
    })

@app.route("/dataManagement/dataInterface/getSrcDataList",methods=["POST"])
def getSrcDataList():
    '''
    '获取打印数据'
    接收 {getList：bool}
    :return:
    '''
    param = request.get_json()
    order = param.get('getList')
    if order:
        databaseList = DataInterface.getDatabaseList()
        return jsonify({'databaseList': databaseList})
    else:
        return jsonify({
            'message':'参数错误，拒绝访问'
        })

@app.route("/dataManagement/dataInterface/updateSrcDataList",methods=["POST"])
def updateSrcDataList():
    '''
    上传源excel文件
    :return:
    '''
    param = request.get_json()
    json_list = param.get('json_list')
    excel_name = param.get('excel_name')
    # print('json_list',json_list)
    # print('excel_name',excel_name)
    DataInterface.json_to_ecxel(json_list,excel_name)
    return jsonify({
        'message':'上传成功！'
    })
@app.route("/dataManagement/dataInterface/deletFile",methods=["POST"])
def delete_excelByFileName():
    '''
    删除源excel文件
    :return:
    '''
    param = request.get_json()
    excel_name = param.get('delete_excel_name')
    DataInterface.deletFile(excel_name)
    return jsonify({
        'message':'删除成功！'
    })

@app.route("/dataManagement/dataInterface/getSrcDataByFileName",methods=["POST"])
def getSrcDataByFileName():
    '''
    通过文件名获取excel文件具体内容
    :return: [{},{}]
    '''
    param = request.get_json()
    excel_name = param.get('excel_name')
    srcDataJson = DataInterface.excel_to_json(excel_name)
    return jsonify({
        'srcDataJson':srcDataJson
    })
#
#
# 元数据管理部分
@app.route("/dataManagement/MetaDataInterface/updateMetaDataList",methods=["POST"])
def updateMetaDataList():
    '''
    上传元excel文件
    :return:
    '''
    param = request.get_json()
    json_list = param.get('json_list')
    excel_name = param.get('excel_name')
    # print('json_list',json_list)
    # print('excel_name',excel_name)
    MetaDataInterface.json_to_ecxel(json_list=json_list,excel_name=excel_name)
    return jsonify({
        'message':'上传成功！'
    })

@app.route("/dataManagement/MetaDataInterface/getMetaDataList",methods=["POST"])
def getMetaDataList():
    '''
    '获取打印数据'
    接收 {getList：bool}
    :return:
    '''
    param = request.get_json()
    order = param.get('getList')
    if order:
        databaseList = MetaDataInterface.getDatabaseList()
        return jsonify({'databaseList': databaseList})
    else:
        return jsonify({
            'message':'参数错误，拒绝访问'
        })

@app.route("/dataManagement/MetaDataInterface/deletMetaFile",methods=["POST"])
def deleteMeta_excelByFileName():
    '''
    删除源excel文件
    :return:
    '''
    param = request.get_json()
    excel_name = param.get('delete_excel_name')
    MetaDataInterface.deletFile(excel_name)
    return jsonify({
        'message':'删除成功！'
    })

@app.route("/dataManagement/MetaDataInterface/getMetaDataByFileName",methods=["POST"])
def getMetaDataByFileName():
    '''
    通过文件名获取excel文件具体内容
    :return: [{},{}]
    '''
    param = request.get_json()
    excel_name = param.get('excel_name')
    MetaDataJson = MetaDataInterface.excel_to_json(excel_name)
    return jsonify({
        'srcDataJson':MetaDataJson
    })
#

@app.route("/dataManagement/MetaDataInterface/getMetaDataAnalysisByFileName",methods=["POST"])
def getMetaDataAnalysisByFileName():
    '''
    通过文件名获取excel文件具体内容
    :return: [{},{}]
    '''
    param = request.get_json()
    excel_name = param.get('excel_name')
    MetaDataAnalysisResult = MetaDataInterface.data_analysis(excel_name)
    return jsonify({
        'MetaDataAnalysisResult':MetaDataAnalysisResult
    })

@app.route("/featuresConstruct/PearsonAnalysis/getClassificationByThreshold",methods=["POST"])
def getClassificationByThreshold():
    '''
    通过文件名,列名和阈值获取pearson强相关系数列表
    :return: [[],[]]
    '''
    param = request.get_json()
    excel_name = param.get('excel_name')
    print(excel_name)
    colums_name = param.get('colums_name')
    threshold = param.get('threshold')
    df = MetaDataInterface.readExcelToDataFrameByFileName(excel_name) #读取对应excel文件
    df_features = df.loc[:,colums_name] #选择要计算的列数据
    df_corr_matrix=pearson.calculatePearson(df_features=df_features)
    corr_features_list=pearson.classificationByThreshold(Threshold=float(threshold),df_corr_matrix=df_corr_matrix)
    figdata_str = pearson.plotHeatMap(df_features=df_features)
    return jsonify({
        'corr_features_list':corr_features_list,

        'figdata_str':figdata_str
    })

if __name__ == '__main__':
    app.run(host="0.0.0.0",debug=True)